/**
 * Parser holder for all parser functions for all modules
 * and all global variables for parsing such as lang and skin
 * Initialized in java parse engine
 */
//Parser = function Parser() {};

/**
 * translate json array to html
 * @param ar
 * @param props
 * @returns {String}
 */
Parser["parseForm"] = function(formtype, env, arguments){
	//parse form
	var funckey = "form:"+formtype+":"+Parser.formskin+":"+Parser.impl+":html";
	Parser.log("finding function: "+funckey);
	var parserfunc = Parser[funckey];
	!parserfunc && Parser.warning("Failed to find function with signature: "+funckey);
	var html = parserfunc(env, arguments);
	return html;
};

/**
 * translate json array to html
 * @param ar
 * @param props
 * @returns {String}
 */
Parser["parseModules"] = function(ar){
	//parse modules
	var s = "";
	if (!ar || !ar.length) {
		return s;
	}
	Parser.log("form conf: "+JSON.stringify(ar));
	var funckey = ar[0].compType+":"+ar[0].type+":"+Parser.formskin+":"+Parser.impl+":html";
	Parser.log("finding function: "+funckey);
	var parserfunc = Parser[funckey];
	!parserfunc && Parser.warning("Failed to find function with signature: "+funckey);
	return parserfunc(ar);
};

/*Parser["parseModules"] = function(root, props){
	var s = "";
	var ar = root.items;
	if (!ar || !ar.length) {
		return s;
	}
	for (var i in ar) {
		var tr = {"tag":"i"};
		var funckey = ar[i].compType+":"+ar[i].type+":"+Parser.formskin+":"+Parser.impl+":html";
		Parser.log("finding function: "+funckey);
		var parserfunc = Parser[funckey];
		!parserfunc && Parser.warning("Failed to find function with signature: "+funckey);
		var prop1 = ar[i].id ? (props[ar[i].id] || {}):{};
		Parser.parseDataProvider(prop1);
		tr.html = parserfunc(ar[i], prop1, root, root.id ? (props[root.id] || {}):{});
		Parser.log("got html:"+tr.html);
		prop1.children = this.parseModules(ar[i], props);
		var wrapper = json2html.transform(prop1, tr);
		s += wrapper.substr(0, wrapper.length-4).substr(3);
		//parse events for this control
		ar[i].id && Parser.parseEvents(ar[i].id);
	}
	return s;
};*/

//parse events for all controls
/*Parser["parseEvents"] = function(mid) {
	if (!Parser.props[mid]) return;
	var estr = "";
	for (var e in Parser.props[mid]) {
		var v = Parser.props[mid][e];
		if (typeof(v)=="object" && v.proptype=='event' && v.value) {
			estr && (estr+=',');
			estr += '"'+e+'":function('+(v.arguments?v.arguments:'')+'){' + v.value+ '}';
		}
	}
	estr && (Parser.generatedscript += '\n\n$("#'+mid+'").on({'+estr+'});');
};*/
Parser["parseEvents"] = function() {
	for (var mid in Parser.props) {
		Parser.log("Parser.parseEvents mid="+mid);
		if (!Parser.props[mid]) return;
		var estr = "";
		for (var e in Parser.props[mid]) {
			var v = Parser.props[mid][e];
			if (v && typeof(v)=="object" && v.proptype=='event' && v.value) {
				estr && (estr+=',');
				estr += '"'+e+'":function('+(v.arguments?v.arguments:'')+'){' + v.value+ '}';
			}
		}
		estr && (Parser.generatedscript += '\n\n$("#'+mid+'").ready(function(){$("#'+mid+'").bind({'+estr+'});});');
		Parser.log("Parser.parseEvents generatedscript="+mid+"="+Parser.generatedscript);
	}
};


//-----------------------------   Utility variables and functions   ---------------------------------

//translate css display from properties value
Parser["cssDisplay"] = function(display, hidefor) {
	if (hidefor && hidefor.displayValue) {
		return 'display:none;';
	} else {
		if ('displaynone'==display) {
			return 'display:none;';
		} else if ('displayblockinline'==display) {
			return 'display:inline;';
		} else if ('visibilityhidden'==display) {
			return 'visibility:hidden;';
		} else {
			return '';
		}
	}
};

//parse if hide for specified roles
Parser["parseHidefor"] = function(prop) {
	var hidefor = "";
	//Parser.log("prop.hidefor");
	//Parser.log(JSON.stringify(prop.hidefor));
	if (prop.hidefor && prop.hidefor.displayValue) {
		if (prop.hidefor.roles && prop.hidefor.roles.length) {
			for (var i=0; i<prop.hidefor.roles.length; i++) {
				if (i>0) hidefor += "|";
				hidefor += prop.hidefor.roles[i].realValue;
			}
		}
	}
	return hidefor;
};

//parse data provider
Parser["parseDataProvider"] = function(id) {
	var prop = Parser.props[id];
	var dataprovider = prop["dataprovider"];
	if (!dataprovider) return;
	var dpval = prop[prop["dataprovider"]];
	
	var dp = {"rtn": dpval.returnType || "enu"};//默认返回枚举类型
	if (dataprovider=="dataprovider-restsrv") {
		dp.dp = "svc";
		dp.intfvars = dpval.intfvars;
		dp.svcid = dpval.realValue;
	} else if (dataprovider=="dataprovider-dict") {
		dp.dp = "dict";
		dp.type = dpval.type;
		dp.target = "BPM";
	} else {
		return;
	}
	Parser.fmdv.dataprovider[id] = dp;
};

//parse autocomplete dataprovider
Parser["parseAutoCompleteDataProvider"] = function(id) {
	var prop = Parser.props[id];
	var autocomplete = prop["autocomplete"];
	Parser.log("parseAutoCompleteDataProvider autocomplete="+autocomplete);
	if (autocomplete=="1") {
		var dataprovider = prop["autocomplete-dataprovider"];
		if (!dataprovider) return;
		var dpval = prop[prop["autocomplete-dataprovider"]];
		
		var dp = {"rtn": dpval.returnType || "enu"};//默认返回枚举类型
		if (dataprovider=="acd-dataprovider-restsrv") {
			dp.dp = "svc";
			dp.intfvars = dpval.intfvars;
			dp.svcid = dpval.realValue;
		} else if (dataprovider=="acd-dataprovider-dict") {
			dp.dp = "dict";
			dp.type = dpval.type;
			dp.target = "BPM";
		} else {
			Parser.log("parseAutoCompleteDataProvider dataprovider="+dataprovider);
			return;
		}
		Parser.fmdv.dataprovider[id+"-autocomplete"] = dp;
	}
};

//parse validator
Parser["parseValidator"] = function(id) {
	Parser.log("parseValidator for "+id);
	var prop = Parser.props[id];
	Parser.log(JSON.stringify(prop));
	var vk;	//validation propname
	var vv;	//validation content value
	var required = false;
	for (var pid in prop) {
		if (prop[pid] && typeof(prop[pid])=="object" && prop[pid].type == 'validation' ) {
			vk = pid;
			vv = prop[pid];
		} else if (pid.indexOf('validation')>-1) {
			if (prop[pid] && typeof(prop[pid])=="string") {
				var vjo = JSON.parse(prop[pid]);
				if (vjo.type == 'validation') {
					vk = pid;
					vv = vjo;
				}
			}
		}
	}
	if (!vk) return;
	//parse
	var vc = ''; //validation output content
	//parse gui validation
	if (vv.gui && vv.gui.rows) {
		for (var i in vv.gui.rows) {
			if (vv.gui.rows[i].data[0]=='NotEmpty') {
				required = true;
			}
			vc += 'fmdf.validations["'+id+'"].push({inputindex:'+vv.inputindex+',func:function' + vv.gui.rows[i].data[2] + ',c:"' + vv.gui.rows[i].data[1] + '",m:"' + vv.gui.rows[i].data[Parser.lang=='zh'?3:4] + '"});\n';
		}
	}
	//parse custom validation
	if (vv.custom && vv.custom.rows) {
		for (var i in vv.custom.rows) {
			vc += 'fmdf.validations["'+id+'"].push({inputindex:'+vv.inputindex+',func:function(v){' + vv.custom.rows[i].data[0] + '},m:"' + vv.custom.rows[i].data[Parser.lang=='zh'?1:2] + '"});\n';
		}
	}
	
	if (vc) {
		Parser.generatedscript += 'fmdf.validations["'+id+'"]=fmdf.validations["'+id+'"]||[];\n' + vc;
	}
	
	return required;
};

//parse tipsy
Parser["parseI18ntip"] = function(id) {
	Parser.generatedscript += '\n\n$("#'+id+'").ready(function(){$("#'+id+'").tipsy({gravity: "e", title:"title"});});';
	Parser.log("Parser.parseI18ntip generatedscript="+id+"="+Parser.generatedscript);
}

